package com.example.myview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.utils.DensityUtil;


/**
 * 用于放大镜
 */
public class MsgView2 extends View {
    private Bitmap mBitmap;
    Context context;
    public MsgView2(Context context) {
        super(context);
        this.context=context;
        setDrawingCacheEnabled(true);
        initView();
    }

    public MsgView2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setDrawingCacheEnabled(true);
        this.context=context;
        initView();
    }
    private void initView()
    {
        mBitmap=Bitmap.createBitmap(80,80, Bitmap.Config.ARGB_8888);
    }
    public void setImageBitmap(Bitmap b)
    {
        DensityUtil du=new DensityUtil(context);
        int width=du.dip2px(80);
        width=width-50;


        int w=b.getWidth();
     int h=b.getHeight();
    float sx=(float)width/w;//要强制转换，不转换我的在这总是死掉。
    float sy=(float)width/h;
    Matrix matrix = new Matrix();
    matrix.postScale(sx, sy); // 长和宽放大缩小的比例
    b= Bitmap.createBitmap(b, 0, 0, w,h, matrix, true);
        this.mBitmap=b;

        invalidate();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        DensityUtil du=new DensityUtil(context);
        int width=du.dip2px(80);



        Path mPath=new Path();

        Paint paint2=new Paint();
        paint2.setAntiAlias(true);
        paint2.setColor(0xffA1ADC8);

        paint2.setStyle(Paint.Style.FILL_AND_STROKE);

       mPath.addCircle(width/2, width/2, width/2, Path.Direction.CCW);
       canvas.clipPath(mPath);
        canvas.drawCircle(width/2,width/2,width/2,paint2);
       canvas.drawBitmap(mBitmap, 0, 0, paint);


    }

}
